﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow2.Portal.Models;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminOSController : BaseController
    {
        private IUser_X user;
        private IOS os;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)os).Dispose();
        }

        [InjectionConstructor]
        public AdminOSController(IUser_X user, IOS os)
        {
            this.user = user;
            this.os = os;
        }

        public ActionResult Add()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            return View();
        }

        [HttpPost]
        public ActionResult Add(OSModel model)
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            bool exsit = os.IsExist(model.OSName);

            if (!exsit)
            {
                os.Add(model);
                ViewBag.Post = true;
            }
            else 
            {
                ViewBag.Post = false;
                ViewBag.error = "该操作系统已存在";
            }
            
            return View(model);
        }

        public ActionResult Edit(int osID)
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            OSModel model = os.GetOSByID(osID);
            return PartialView(model);
        }

        [HttpPost]
        public void Edit(OSModel model)
        {
            os.Update(model);
        }

        [HttpPost]
        public void Delete(int id)
        {
            os.Delete(id);
        }

        [HttpPost]
        public void Publish(int id)
        {
            //do something about the publish
        }

    }
}
